package com.szy.douyou_article.web;

import com.szy.douyou_article.config.ResultDTO;
import com.szy.douyou_article.dao.Friend;
import com.szy.douyou_article.service.FriendService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author sunziyan
 * @date 2022/4/19
 */
@RestController
@RequestMapping("/friend")
public class FriendController {
    @Resource
    private FriendService friendService;

    @PostMapping("/saveChatHistory")
    public ResultDTO saveChatHistory(@RequestBody Friend friend){

        return friendService.saveChatHistory(friend);
    }

    @GetMapping("/getChatHistory/{userId}/{friendId}")
    public ResultDTO getChatHistory(@PathVariable("userId")Long userId,@PathVariable("friendId")Long friendId){
        Friend friend = new Friend();
        friend.setFriendId(friendId);
        friend.setUserId(userId);
        return friendService.getChatHistory(friend);
    }

    /**
     * 添加好友
     * @param userId
     * @param friendId
     * @return
     */
    @GetMapping("/addFriend/{userId}/{friendId}")
    public ResultDTO addFriend(@PathVariable("userId")Long userId,@PathVariable("friendId")Long friendId){
        Friend friend = new Friend();
        friend.setFriendId(friendId);
        friend.setUserId(userId);
        //去数据库查看是否已添加过好友，如果没有，添加两条记录
        if(friendService.getChatHistory(friend).getSuccess() != false){
            return ResultDTO.fail("你已经添加该好友！");
        }
        //添加好友
        return friendService.insertFriend(friend);

    }

    @GetMapping("/getMyFriend/{userId}/{pageNo}/{pageSize}")
    public ResultDTO getMyFriend(@PathVariable("userId")Long userId,@PathVariable("pageNo")int pageNo,@PathVariable("pageSize")int pageSize){
        return friendService.getMyFriend(userId,pageNo,pageSize);
    }

}
